package com.zhang.oauth2;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.http.MediaType;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghaidong
 * @version v1.0
 * @description
 *
 *    GET 请求
 *    http://localhost:8099/oauth/token?username=zhd&password=111111&grant_type=password&scope=webClient&client_id=appClient&client_secret=123456
 *
 *    响应
 *    {
 *     "access_token": "5a146a63-da86-472a-b7a8-9b7640bf058c",
 *     "token_type": "bearer",
 *     "refresh_token": "eca7b223-5aa8-4d3b-89b5-aff39c194272",
 *     "expires_in": 43199,
 *     "scope": "webClient"
 *    }
 *
 *
 *    集成jwt
 *    http://localhost:8099/oauth/token?username=zhd&password=111111&grant_type=password&scope=webClient&client_id=appClient&client_secret=123456
 *    {
 *     "access_token": "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjcwMTg0MTQsInVzZXJfbmFtZSI6InpoZCIsImF1dGhvcml0aWVzIjpbIlJPTEVfVVNFUiJdLCJqdGkiOiJmNjc4NGZjOC0xYjBhLTQ4OTMtOGViNi0zZDdkZjk0MWRmYTAiLCJjbGllbnRfaWQiOiJhcHBDbGllbnQiLCJzY29wZSI6WyJ3ZWJDbGllbnQiXX0.jUKAuitsoGTTLzhTO1SZC_4S_UCkn_N3iHU-BT9zDD0",
 *     "token_type": "bearer",
 *     "refresh_token": "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX25hbWUiOiJ6aGQiLCJzY29wZSI6WyJ3ZWJDbGllbnQiXSwiYXRpIjoiZjY3ODRmYzgtMWIwYS00ODkzLThlYjYtM2Q3ZGY5NDFkZmEwIiwiZXhwIjoxNTY5NTY3MjE0LCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiNzIyMjE3ZGEtMmM4ZS00YzhhLWE4YTItMjg3ZTk1ZTgzNWU5IiwiY2xpZW50X2lkIjoiYXBwQ2xpZW50In0.sUyZxCA7ZdAXKIVbgbDboxC_E-AFctbe4u-mqwvv9HY",
 *     "expires_in": 43199,
 *     "scope": "webClient",
 *     "jti": "f6784fc8-1b0a-4893-8eb6-3d7df941dfa0"
 *    }
 * @date 2019/8/28
 */
@Slf4j
@EnableEurekaClient
@EnableResourceServer
@EnableAuthorizationServer
@RestController
@SpringBootApplication
public class Oauth2ServerApplicationV {

    public static void main(String[] args) {
        SpringApplication.run(Oauth2ServerApplicationV.class, args);
    }

    @RequestMapping(value = "/auth/user", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> user(OAuth2Authentication user) {
        log.info("-----------------------request /auth/user------------------------");
        log.info("param = {}", JSON.toJSONString(user));
        Map<String, Object> userInfo = new HashMap<>(2);
        userInfo.put("user", user.getUserAuthentication().getPrincipal());
        userInfo.put("authorities", AuthorityUtils.authorityListToSet(user.getUserAuthentication().getAuthorities()));
        return userInfo;
    }
}
